//
//  GoodDetailPicsView.m
//  quxiangtao
//
//  Created by xbmios on 2018/7/26.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "GoodDetailPicsView.h"
#import <WebKit/WebKit.h>

@interface GoodDetailPicsView ()<WKUIDelegate, WKNavigationDelegate>

@property (strong, nonatomic) WKWebView *webView;

@end

@implementation GoodDetailPicsView

- (instancetype)init {
    
    if (self = [super init]) {
        
        [self addSubview:self.webView];
        
        self.backgroundColor = [UIColor blueColor];
    
        __weak typeof(self) weakSelf = self;
        [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(weakSelf.mas_left);
            make.right.mas_equalTo(weakSelf.mas_right);
            make.top.mas_equalTo(weakSelf.mas_top);
            make.bottom.mas_equalTo(weakSelf.mas_bottom);
       
        }];
        
   
    }
    
    return self;

}


- (void)setHtmlURL:(NSString *)htmlURL {
    
    _htmlURL = htmlURL;
    
    NSString *str = [NSString stringWithFormat:@"%@",htmlURL];
    str = [str stringByReplacingOccurrencesOfString:@"\\\"" withString:@"\""];
    str = [str stringByReplacingOccurrencesOfString:@"\\/" withString:@"/"];
    str = [str stringByReplacingOccurrencesOfString:@"\\n" withString:@""];
    str = [str stringByReplacingOccurrencesOfString:@"\\t" withString:@""];
    str = [str stringByReplacingOccurrencesOfString:@"\"" withString:@""];
    str = [str stringByReplacingOccurrencesOfString:@"\\r" withString:@""];

    [self.webView loadHTMLString:str baseURL:nil];
    
}


- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
    
    WKNavigationResponsePolicy actionPolicy = WKNavigationResponsePolicyAllow;
    //这句是必须加上的，不然会异常
    decisionHandler(actionPolicy);
    
}

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    
    WKNavigationActionPolicy actionPolicy = WKNavigationActionPolicyAllow;
    
    decisionHandler(actionPolicy);
    
}

/// 2 页面开始加载
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    
    
}

/// 4 开始获取到网页内容时返回
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
    
    
}

/// 5 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    
    
}

#pragma mark -lazy
- (WKWebView *)webView {
    
    if (!_webView) {
        
        _webView = [[WKWebView alloc] init];
        
        _webView.UIDelegate = self;
        _webView.navigationDelegate = self;
        
        _webView.backgroundColor = Colorf2f2f2;
        _webView.scrollView.scrollEnabled = YES;
        _webView.scrollView.showsVerticalScrollIndicator = NO;
        _webView.scrollView.showsHorizontalScrollIndicator = NO;
        
        if (@available(iOS 11.0 , *)) {
            
            _webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            
        }
        
    }
    
    return _webView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
